package com.zhuoyue.system.service.impl;

import com.zhuoyue.common.annotation.DataScope;
import com.zhuoyue.common.core.domain.ChartData;
import com.zhuoyue.common.utils.DateUtils;
import com.zhuoyue.system.domain.po.SelectTimePo;
import com.zhuoyue.system.mapper.*;
import com.zhuoyue.system.service.ISysIndexPageDataService;
import com.zhuoyue.system.service.ITblAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhanghui
 */
@Service
public class SysIndexPageDataServiceImpl implements ISysIndexPageDataService {
    @Autowired
    private TblAppUserMapper appUserMapper;

    @Autowired
    private TblLogManageMapper logManageMapper;

    @Autowired
    private TblUserReqCountMapper userReqCountMapper;

    @Autowired
    private SysMessageMapper sysMessageMapper;

    @Autowired
    private TblDeviceMapper tblDeviceMapper;

    @Autowired
    private SysProductManageMapper productManageMapper;

    @Autowired
    private TblGatewayZyMapper gatewayZyMapper;

    @Autowired
    private ITblAppUserService tblAppUserService;


    @Override
    public int getYesterdayRegisterCount(SelectTimePo selectTimePo) {
        List<Date> yesterdayStartAndEndDate = DateUtils.getYesterdayStartAndEndDate();
        selectTimePo.setSelectStartTime(yesterdayStartAndEndDate.get(0));
        selectTimePo.setSelectEndTime(yesterdayStartAndEndDate.get(1));
        return tblAppUserService.getRegisterCount(selectTimePo);
    }

    @DataScope(deptAlias = "s_d")
    @Override
    public List<ChartData> getRegisterChartData(SelectTimePo selectTimePo) {
        List<ChartData> sevenDaysData = new ArrayList<>(7);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        // 前七天的日期字符串
        List<String> sevenDaysBeforeToday = DateUtils.getDateBeforeToday(7);
        // 最近八天的数据
        List<ChartData> registerCountOfDays = appUserMapper.getRegisterCountOfDays(selectTimePo);

        boolean flag = false;
        for (String oneDay : sevenDaysBeforeToday) {
            for (ChartData registerCountOfDay : registerCountOfDays) {
                if (registerCountOfDay.getTime().equals(oneDay)) {
                    registerCountOfDay.setTime(oneDay);
                    sevenDaysData.add(registerCountOfDay);
                    flag = true;
                    break;
                }
            }

            if (flag) {
                flag = false;
                continue;
            }

            //若这天没有数据
            ChartData chartData = new ChartData();
            chartData.setTime(oneDay);
            chartData.setCount(0);
            sevenDaysData.add(chartData);
        }

        return sevenDaysData;
    }

    @DataScope(deptAlias = "s_d")
    @Override
    public int getYesterdayActiveCount(SelectTimePo selectTimePo) {
        List<Date> yesterdayStartAndEndDate = DateUtils.getYesterdayStartAndEndDate();
        selectTimePo.setSelectStartTime(yesterdayStartAndEndDate.get(0));
        selectTimePo.setSelectEndTime(yesterdayStartAndEndDate.get(1));
        return logManageMapper.getLoginUserCount(selectTimePo);
    }

    @Override
    public List<ChartData> getActiveChartData(String orgCode) {
        List<ChartData> sevenDaysData = new ArrayList<>(7);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        // 前七天的日期字符串
        List<String> sevenDaysBeforeToday = DateUtils.getDateBeforeToday(7);
        // 最近八天的数据
        List<ChartData> activeCountOfDays = logManageMapper.getActiveCountOfDays(orgCode);


        boolean flag = false;
        for (String oneDay : sevenDaysBeforeToday) {
            for (ChartData activeCountOfDay : activeCountOfDays) {
                if (activeCountOfDay.getTime().equals(oneDay)) {
                    activeCountOfDay.setTime(oneDay);
                    sevenDaysData.add(activeCountOfDay);
                    flag = true;
                    break;
                }
            }

            if (flag) {
                flag = false;
                continue;
            }

            //若这天没有数据
            ChartData chartData = new ChartData();
            chartData.setTime(oneDay);
            chartData.setCount(0);
            sevenDaysData.add(chartData);
        }

        return sevenDaysData;
    }

    @DataScope(deptAlias = "s_d")
    @Override
    public Integer getApiAccessCount(SelectTimePo selectTimePo) {
        Integer accessCount = userReqCountMapper.getAccessCount(selectTimePo);
        if (accessCount == null) {
            accessCount = 0;
        }
        return accessCount;
    }

    @DataScope(deptAlias = "s_d")
    @Override
    public int getMessageCount1(SelectTimePo selectTimePo) {
        return sysMessageMapper.getMeteringLogCount(selectTimePo);
    }

    @DataScope(deptAlias = "s_d")
    @Override
    public int getMessageCount2(SelectTimePo selectTimePo) {
        return sysMessageMapper.getSecurityLogCount(selectTimePo);
    }

    @DataScope(deptAlias = "s_d")
    @Override
    public int getMessageCount3(SelectTimePo selectTimePo) {
        return sysMessageMapper.getSensirionLogCount(selectTimePo);
    }

    @DataScope(deptAlias = "s_d")
    @Override
    public int getMessageCount4(SelectTimePo selectTimePo) {
        return sysMessageMapper.getTblMessageCount(selectTimePo);
    }

    @DataScope(deptAlias = "s_d")
    @Override
    public int getMessageCount5(SelectTimePo selectTimePo) {
        return logManageMapper.getUserLoginLogCountByOrgCode(selectTimePo);
    }

    @DataScope(deptAlias = "s_d")
    @Override
    public int getMessageCount6(SelectTimePo selectTimePo) {
        return userReqCountMapper.getUserApiReqCountByOrgCode(selectTimePo);
    }

    @DataScope(deptAlias = "s_d")
    @Override
    public List<ChartData> getDeviceChartData(SelectTimePo selectTimePo) {

        List<ChartData> sevenDaysData = new ArrayList<>(7);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        // 前七天的日期字符串
        List<String> sevenDaysBeforeToday = DateUtils.getDateBeforeToday(7);
        // 最近八天的数据
        List<ChartData> activeCountOfDays = tblDeviceMapper.getDeviceCountOfDays(selectTimePo);


        boolean flag = false;
        for (String oneDay : sevenDaysBeforeToday) {
            for (ChartData activeCountOfDay : activeCountOfDays) {
                if (activeCountOfDay.getTime().equals(oneDay)) {
                    activeCountOfDay.setTime(oneDay);
                    sevenDaysData.add(activeCountOfDay);
                    flag = true;
                    break;
                }
            }

            if (flag) {
                flag = false;
                continue;
            }

            //若这天没有数据
            ChartData chartData = new ChartData();
            chartData.setTime(oneDay);
            chartData.setCount(0);
            sevenDaysData.add(chartData);
        }

        return sevenDaysData;
    }

    @DataScope(deptAlias = "s_d")
    @Override
    public List<ChartData> getTopTenDeviceChartData(SelectTimePo selectTimePo) {
        List<ChartData> topTenDeviceList = tblDeviceMapper.getTopTenDeviceCount(selectTimePo);
        for (ChartData chartData : topTenDeviceList) {
            String categoryName = productManageMapper.getCategoryNameByNum(chartData.getTime());
            if (categoryName != null) {
                chartData.setTime(categoryName);
            }
        }
        return topTenDeviceList;
    }

    @Override
    public int getProductInfoCount(String orgCode) {
        return productManageMapper.getProductInfoCount(orgCode);
    }

    @DataScope(deptAlias = "s_d")
    @Override
    public List<ChartData> getGatewayGeoChartData(SelectTimePo selectTimePo) {
        return gatewayZyMapper.getGatewayGeoChartData(selectTimePo);
    }
}
